#1.拿到主页面的源代码 然后提取子页面的链接地址,href
#2.通过href拿到子页面的内容,从子页面中找到图片的下载地址 img->src属性
#3.下载图片

import  requests
from bs4 import BeautifulSoup
import  time

url = "http://umei.cc/weimeitupian/keaitupian/"
response = requests.get(url)
response.encoding = 'utf-8'
page_text = response.text

soup = BeautifulSoup(page_text,'html.parser')
parent = soup.find("div",attrs={"id":"infinite_scroll"})
divs = parent.find_all("div",attrs={"class":"item masonry_brick"})
for div in divs:
    a = div.find("div",attrs={"class":"img"}).find("a")
    href = a.get("href")#通过get获取属性
    href = "http://umei.cc"+href
    #拿到子页面的源代码
    child_req = requests.get(href)
    child_req.encoding = 'utf-8'
    child_page_text  = child_req.text
    # 从子页面中拿到下载链接
    child_soup = BeautifulSoup( child_page_text,'html.parser')
    child_img = child_soup.find("div",attrs={"class":"big-pic"}).find("img")
    src = child_img.get("src")
    #3.下载图片
    # print(src)
    img_req = requests.get(src)
    img_name = src.split("/")[-1]
    # img_req.content # 拿到的是字节
    with open("img/"+img_name, mode="wb") as f:
        f.write(img_req.content) #图片内容写入文件
        f.close()
    print(img_name,"over!!!")
    time.sleep(1)
print("all,over")

